package com.baway.base.view

import android.os.Bundle
import android.os.PersistableBundle
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding

/**
 * Author: Jason Zhang
 * Date: 2025/8/16
 * Desc: 基础Activity
 */
abstract class BaseActivity<VDB: ViewDataBinding> : AppCompatActivity() {

    protected lateinit var mDataBinding: VDB

    /**
     * 参数savedInstanceState，用于获取Activity的实例状态
     * 场景：当Activity重新创建（屏幕旋转，配置更新）
     */
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //使用DataBinding设置布局
        mDataBinding = DataBindingUtil.setContentView(this, getLayoutId())
        //初始化UI
        initViews()
    }

    /**
     * 获取Activity重建后的实例状态参数
     */
    override fun onRestoreInstanceState(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
        super.onRestoreInstanceState(savedInstanceState, persistentState)
        savedInstanceState?.run {
            getActivityState(this)
        }
    }

    /**
     * 保存当前Activity的状态
     * 屏幕旋转，配置更新
     */
    override fun onSaveInstanceState(outState: Bundle, outPersistentState: PersistableBundle) {
        super.onSaveInstanceState(outState, outPersistentState)
        saveActivityState(outState)
    }

    protected open fun getActivityState(state: Bundle){

    }

    protected open fun saveActivityState(state: Bundle){

    }

    protected abstract fun getLayoutId(): Int

    protected open fun initViews(){

    }




}